﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopyUnit
{
    //http://www.cnblogs.com/nliao/archive/2012/11/18/2776114.html
    public class Student : IDeepCopy, IShallowCopy
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public ClassRoom Room = new ClassRoom();
        public Student() { }
        public Student(string name, int age)
        {
            this.Age = age;
            this.Name = name;
        }
        public object ShallowCopy()
        {
            return this.MemberwiseClone();
        }
        public object DeepCopy()
        {
            Student s = new Student();
            s.Name = this.Name;
            s.Age = this.Age;
            s.Room = (ClassRoom)this.Room.DeepCopy();
            return s;
        }
        public override string ToString()
        {
            return string.Format("Name:{0}\tAge:{1}\t{2}", this.Name, this.Age, this.Room.ToString());
        }
    }
    public class ClassRoom : IDeepCopy, IShallowCopy
    {
        public int RoomId = 1;
        public string RoomName = "Room1";

        public override string ToString()
        {
            return string.Format("RoomId={0}\tRoomName={1}", RoomId, RoomName);
        }
        public object ShallowCopy()
        {
            ClassRoom r = new ClassRoom();
            r.RoomId = this.RoomId;
            r.RoomName = this.RoomName;
            return r;
        }

        public object DeepCopy()
        {
            //直接使用内置的浅拷贝方法返回
            return this.MemberwiseClone();
        }
    }
    public class CopyTest
    {
        public object obj { get; set; }
    }
}
